package com.device_manager.service.impl;

import com.device_manager.dto.DeviceModelDTO;
import com.device_manager.dto.PageDeviceModelDTO;
import com.device_manager.entity.DeviceModel;
import com.device_manager.mapper.DeviceModelMapper;
import com.device_manager.result.PageResult;
import com.device_manager.service.DeviceModelService;
import com.device_manager.service.OssService;
import com.device_manager.vo.PageDeviceModelVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DeviceModelServiceImpl implements DeviceModelService {
    @Autowired
    private  OssService ossService;
    @Autowired
    private DeviceModelMapper deviceModelMapper;
    /**
     * 添加设备型号
     * @param deviceModelDTO
     */
    @Override
    public void CreateDeviceModel(DeviceModelDTO deviceModelDTO) {
        // 处理文件上传
        String imageUrls = null;
        if (deviceModelDTO.getDeviceImage() != null) {
            imageUrls = ossService.uploadFile(deviceModelDTO.getDeviceImage(), "images/");
        }
        DeviceModel deviceModel = new DeviceModel();
        BeanUtils.copyProperties(deviceModelDTO, deviceModel);
        deviceModel.setCreatedTime(LocalDateTime.now());
        deviceModel.setDeviceImage(imageUrls);
        deviceModelMapper.CreateDeviceModel(deviceModel);
    }
    /**
     * 删除设备型号
     * @param deviceModelDTO
     */
    @Override
    public void deleteDeviceModel(DeviceModelDTO deviceModelDTO) {
        deviceModelMapper.deleteDeviceModel(deviceModelDTO);
    }
    /**
     * 修改设备型号
     * @param deviceModelDTO
     */
    @Override
    public void updateDeviceModel(DeviceModelDTO deviceModelDTO) {
        String imageUrls = null;
        if (deviceModelDTO.getDeviceImage() != null) {
            imageUrls = ossService.uploadFile(deviceModelDTO.getDeviceImage(), "images/");
        }
        DeviceModel deviceModel = new DeviceModel();
        BeanUtils.copyProperties(deviceModelDTO, deviceModel);
        deviceModel.setUpdatedTime(LocalDateTime.now());
        deviceModel.setDeviceImage(imageUrls);
        deviceModelMapper.updateDeviceModel(deviceModel);
    }
    /**
     * 查询设备型号列表
     * @param pageDeviceModelDTO
     * @return
     */
    @Override
    public PageResult<PageDeviceModelVO> deviceModelList(PageDeviceModelDTO pageDeviceModelDTO) {
        PageHelper.startPage(pageDeviceModelDTO.getPage(),pageDeviceModelDTO.getPageSize());
        Page<PageDeviceModelVO> deviceModels = deviceModelMapper.deviceModelList(pageDeviceModelDTO);
        long  total   = deviceModels.getTotal();
        List  records = deviceModels.getResult();
        return new  PageResult<>(total,records);
    }
    /**
     * 查询设备型号详情
     * @param modelId
     * @return
     */
    @Override
    public PageDeviceModelVO deviceModelDetail(Long modelId) {

        return  deviceModelMapper.deviceModelDetail(modelId);
    }


}
